package controllers;

import dto.RelatedTopicDto;
import dto.SitemapDto;
import helpers.InSitemap;
import models.*;
import org.apache.commons.lang.StringUtils;
import play.Play;
import play.i18n.Lang;
import play.mvc.Before;
import play.mvc.Controller;
import play.mvc.Http;
import play.mvc.Scope;
import services.TopicService;
import utils.InputValidator;

import javax.mail.Session;
import java.io.ByteArrayInputStream;
import java.util.List;

public class User extends Controller{
    static String guchi_language = "guchi_language";
    @Before
    static void setMenu() {
        //System.out.println("setMenu - Will set to VN language");
        //Lang.set("vn");
        if (Scope.Session.current().get(guchi_language)==null) {
            System.out.println("setMenu - First request of session, will set to VN language");
            Scope.Session.current().put(guchi_language, "vn");
            Lang.set("vn");
        }

        //hardcode
        Lang.set("vn");

        //Play.configuration.put("appMenus", MenuService.getAppMenuItems());
        List<AppGroup> appGroups = AppGroup.all(AppGroup.class).filter("language", Lang.get()).order("order").fetch();
        Play.configuration.put("menuAppGroups", appGroups);
        List<Banner> appBanners = Banner.all(Banner.class).filter("language", Lang.get()).filter("type", "MAIN_BANNER").order("order").fetch();
        Banner banner = new Banner();

        if (appBanners != null && appBanners.size()>0){
            banner = appBanners.get(0);
        }

        Play.configuration.put("banner", banner);
    }

    private static void renderArguments(String title, String siteDesc, String siteKeyword){
        renderArgs.put("title", title);
        renderArgs.put("siteDesc", siteDesc);
        renderArgs.put("siteKeyword", siteKeyword);
    }

    private static void renderSeoConfigArguments(SitemapDto page) {
        List<SeoConfig> seoConfigs = SeoConfig.all(SeoConfig.class).filter("page", page.code).fetch();
        if (seoConfigs != null && seoConfigs.size()>0){
            renderArguments(seoConfigs.get(0).title, seoConfigs.get(0).description, seoConfigs.get(0).keyword);
        }else {
            renderArguments("Yamaguchi Việt Nam", "Yamaguchi Việt Nam", "Yamaguchi Việt Nam");
        }
    }

    @InSitemap(changefreq="daily", priority=0.8)
    public static void home() {
        if (StringUtils.isEmpty(Lang.get())) {
            System.out.println("home - Will set default to VN");
            Lang.set("vn");
        }

        //@TODO: need order by date
//        List<AppGroup> allGroupApps = AppGroup.all(AppGroup.class).filter("language", Lang.get()).fetch();

        List<Brand> allBrands = App.all(Brand.class).filter("language", Lang.get()).order("order").fetch();

        List<Banner> allSlides = Banner.all(Banner.class).filter("language", Lang.get()).filter("type", "SLIDE_HOME").fetch();

        renderSeoConfigArguments(SitemapDto.HOME);

        render(allBrands, allSlides);
    }

    @InSitemap(changefreq="daily", priority=0.8)
    public static void topic(String code, String rewriteUrl) {
        Topic topic = Topic.all().filter("code", code).filter("language", Lang.get()).get();
        if (topic==null) {
            flash.error("Doesn't exist topic with code=" + code + " in language=" + Lang.get());
            renderTemplate("User/error.html");
        }
        
        List<RelatedTopicDto> relatedTopics = TopicService.getRelatedTopicDto(topic);

        renderSeoConfigArguments(SitemapDto.PRODUCT);
        if (InputValidator.isNullOrBlank(rewriteUrl))
            redirect("/user/chu-de/" + code + "/" + topic.getShortTitle());
        render(topic, relatedTopics);
    }

    /**
     *
     * @param topicId
     * @param type  : 1 = relatedPhoto, 2 = largePhoto
     */
    public static void getTopicImage(Long topicId, int type) {
        if (topicId==null) return;

        Topic t = Topic.findById(topicId);
        if (t==null) {
            return;
        }

        response.setContentTypeIfNotSet("jpg");
        if (type == 1 && t.relatedPhoto!=null) {
            renderBinary(new ByteArrayInputStream(t.relatedPhoto));
        }

        if (type == 2 && t.largePhoto!=null) {
            renderBinary(new ByteArrayInputStream(t.largePhoto));
        }
    }


    /**
     *
     */
    public static void getAppImage(Long appId) {
        if (appId==null) return;

        App a = App.getByKey(App.class, appId);
        if (a==null) {
            return;
        }

        response.setContentTypeIfNotSet("jpg");
        renderBinary(new ByteArrayInputStream(a.photo));
    }

    public static void getBannerImage(Long bannerId) {
        if (bannerId==null) return;

        Banner a = Banner.getByKey(Banner.class, bannerId);
        if (a==null) {
            return;
        }

        response.setContentTypeIfNotSet("jpg");
        renderBinary(new ByteArrayInputStream(a.photo));
    }

    public static void getAppGroupImage(Long appId) {
        if (appId==null) return;

        AppGroup a = AppGroup.getByKey(AppGroup.class, appId);
        if (a==null) {
            return;
        }

        response.setContentTypeIfNotSet("jpg");
        renderBinary(new ByteArrayInputStream(a.photo));
    }


    public static void getBrandImage(Long id) {
        if (id==null) return;

        Brand a = Brand.getByKey(Brand.class, id);
        if (a==null) {
            return;
        }

        response.setContentTypeIfNotSet("jpg");
        renderBinary(new ByteArrayInputStream(a.logo));
    }
    
    @InSitemap(changefreq="daily", priority=0.8)
    public static void allNews() {
        List<TinTuc> allNews = TinTuc.all(TinTuc.class).filter("language", Lang.get()).fetch();
        System.out.println("All News:"+allNews);
        renderSeoConfigArguments(SitemapDto.NEWS);
        render(allNews);
    }

    @InSitemap(changefreq="daily", priority=0.8)
    public static void aboutUs_vn() {
        renderSeoConfigArguments(SitemapDto.ABOUT_US);
        render();
    }

    @InSitemap(changefreq="daily", priority=0.8)
    public static void aboutUs_en() {
        renderSeoConfigArguments(SitemapDto.ABOUT_US);
        render();
    }

    @InSitemap(changefreq="daily", priority=0.8)
    public static void contact_vn() {
        renderSeoConfigArguments(SitemapDto.CONTACT);
        render();
    }

    @InSitemap(changefreq="daily", priority=0.8)
    public static void contact_en() {
        renderSeoConfigArguments(SitemapDto.CONTACT);
        render();
    }

    @InSitemap(changefreq="daily", priority=0.8)
    public static void changeLanguage(String langCode) {
        System.out.println("will change to " +  langCode + " from " + Lang.get());
        Lang.change(langCode);
        Scope.Session.current().put(guchi_language, langCode);
        System.out.println("Changed to " +  Lang.get());

        renderSeoConfigArguments(SitemapDto.HOME);
        redirect("User.home");
    }

    @InSitemap(changefreq="daily", priority=0.8)
    public static  void detailsNews(Long id, String rewriteUrl){
        TinTuc allInfoNews = TinTuc.getByKey(TinTuc.class, id);
        System.out.println("One news: " + allInfoNews );
        renderSeoConfigArguments(SitemapDto.NEWS);
        render(allInfoNews);
    }

    public static void incoming() {
        render();
    }

    @InSitemap(changefreq="daily", priority=0.8)
    public static void listTopicInApp(String code, String rewriteUrl) {
        App app = App.all(App.class).filter("code", code).get();

        AppGroup appGroup = AppGroup.all(AppGroup.class).filter("language", Lang.get()).filter("code", app.groupCode).get();

        List<Topic> allTopics = Topic.all(Topic.class).filter("language", Lang.get()).filter("appCode", code).order("order").fetch();

        renderSeoConfigArguments(SitemapDto.PRODUCT);
        render(app, appGroup, allTopics);
    }

    @InSitemap(changefreq="daily", priority=0.8)
    public static void listAppInGroup(String code, String rewriteUrl) {
        AppGroup appGroup = AppGroup.all(AppGroup.class).filter("language", Lang.get()).filter("code", code).get();
        List<App> allApps = App.all(App.class).filter("groupCode", code).filter("language", Lang.get()).order("order").fetch();

        renderSeoConfigArguments(SitemapDto.PRODUCT);
        render(appGroup, allApps);
    }
}
